home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '88 / Proceedings '88 / Feldt Advanced Mac Programming / File Manager / Exist test / ssgHFSlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-04  |  1.2 KB  |  70 lines  |  [TEXT/KAHL]

  1.  
  2. #include <hfs.h>
  3. #include "ssghfslib.h"
  4.  
  5. main()
  6. {
  7. char name[100];
  8. int ref, res;
  9. int dir;
  10.  
  11. do
  12.     {
  13.     scanf ("%s %d", name, &dir);
  14.     res = file_exists (name, -2, dir, &ref);
  15.     printf ("\nresult=%d  ref=%d", res, ref);
  16.     }
  17.     while ( name[1] != '`' );
  18.  
  19. }
  20.  
  21. /*
  22.     file_exists: tests to see if a file exists
  23.     
  24.     Passed:
  25.         char *name;            file/folder name - C string
  26.         int  the_vol;        vRefNum, wdRefNum, or 0 for current
  27.         int     the_dir;        dirRefnum, 0 for current of if WD specified
  28.         int  *ref;            returns dirRefNum if is dir and exists (can pass 0L)
  29.         
  30.     Returns:
  31.         int                    1=file exists, 2=folder exists, 0=no exist, -1=I/O err
  32. */
  33. file_exists (name, the_vol, the_dir, ref)
  34.     unsigned char *name;
  35.     int    the_vol;
  36.     int the_dir;
  37.     int *ref;
  38. {
  39.     CInfoPBRec pb;
  40.     unsigned char           temp[50];
  41.     int         error;
  42.     
  43.     CtoPstr ( name);
  44.     pb.dirInfo.ioCompletion = 0L;
  45.     pb.dirInfo.ioFDirIndex = 0;
  46.     pb.dirInfo.ioNamePtr = name;
  47.     pb.dirInfo.ioVRefNum = the_vol;
  48.     pb.dirInfo.ioDrDirID = the_dir;
  49.             
  50.     error = PBGetCatInfo (&pb, FALSE);
  51.     switch ( error )
  52.         {
  53.         case noErr:
  54.             if ( pb.dirInfo.ioFlAttrib & HFS_DIR )    /* dir? */
  55.                 {
  56.                 if ( ref )
  57.                     *ref = pb.dirInfo.ioDrDirID;
  58.                 return ( 2 );
  59.                 }
  60.             else
  61.                 return ( 1 );
  62.         case fnfErr:
  63.             return (0);
  64.         default:
  65.             return (-1);    
  66.         }
  67. }
  68.  
  69.  
  70.